LOCK...UNLOCK Statement ---------------------------------------------------------------------------- Action Controls access by other processes to all or part of an opened file. Syntax LOCK # filenumber% ,{ record& | start& TO end&} . . . UNLOCK # filenumber% ,{ record& | start& TO end&} Remarks These statements are used in networked environments where several processes might need access to the same file. The LOCK and UNLOCK statements use the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- filenumber% The number used in the OPEN statement to open the file. record& The number of the record or byte to be locked. The argument record& can be any number from 1 Argument Description ---------------------------------------------------------------------------- record& can be any number from 1 to 2,147,483,647 (equivalent to 231 -1). A record can be up to 32,767 bytes in length. start& The number of the first record or byte to be locked. end& The number of the last record or byte to be locked.. For binary-mode files, record&, start&, and end& represent the number of a byte relative to the beginning of the file. The first byte in a file is byte 1. For random-access files, record&, start&, and end& represent the number of a record relative to the beginning of the file. The first record is record 1. If the file has been opened for sequential input or output, LOCK and UNLOCK affect the entire file, regardless of the range specified by start& and end&. The LOCK and UNLOCK statements are always used in pairs. The arguments to LOCK and UNLOCK must match exactly. If you specify just one record, then only that record is locked or unlocked. If you specify a range of records and omit a starting record ( start&), then all records from the first record to the end of the range ( end&) are locked or unlocked. LOCK with no record& locks the entire file, while UNLOCK with no record& unlocks the entire file. LOCK and UNLOCK execute only at run time if you are using OS-2 or versions of DOS that support networking (version 3.1 or later). In DOS, you must run the SHARE.EXE program to enable locking operations. Using earlier versions of DOS causes BASIC to generate the error message Feature unavailable if LOCK and UNLOCK are executed. Warning Be sure to remove all locks with an UNLOCK statement before closing a file or terminating your program. Failing to remove locks produces unpredictable results. The arguments to LOCK and UNLOCK must match exactly. Do not use LOCK and UNLOCK on devices or ISAM tables. If you attempt to access a file that is locked, BASIC may generate the following error messages. Bad record number Permission denied Example The following example illustrates the use of the LOCK and UNLOCK statements. The program creates a sample data record in a random-access file. Once the sample record is created and closed, the program again opens the file to allow customer information to be updated. As a record is opened, it is locked to prevent use by another terminal that has access to the same file. After the update is complete, the record is unlocked, again allowing modifications by others with access. ' Define the record. TYPE AccountRec Payer AS STRING * 20 Address AS STRING * 20 Place AS STRING * 20 Owe AS SINGLE END TYPE DIM CustRec AS AccountRec ' This section creates a sample record to use. ON ERROR GOTO ErrHandler OPEN "MONITOR" FOR RANDOM SHARED AS #1 LEN = LEN(CustRec) CustRec.Payer = "George Washington" CustRec.Address = "1 Cherry Tree Lane" CustRec.Place = "Mt. Vernon, VA" CustRec.Owe = 12! PUT #1, 1, CustRec ' Put one record in the file. CLOSE #1 ' This section opens the sample record for updating. OPEN "MONITOR" FOR RANDOM SHARED AS #1 LEN = LEN(CustRec) DO Number% = 0 ' Reset to zero. DO UNTIL Number% = 1 ' Force user to input 1. CLS . LOCATE 10, 10 INPUT "Customer Number? #"; Number% LOOP ' Lock the current record so another process ' doesn't change it while you're using it. LOCK #1, Number% GET #1, Number%, CustRec LOCATE 11, 10. PRINT "Customer. "; CustRec.Payer LOCATE 12, 10. PRINT "Address. "; CustRec.Address LOCATE 13, 10. PRINT "Currently owes. $"; CustRec.Owe LOCATE 15, 10. INPUT "Change (+ or -)", Change! CustRec.Owe = CustRec.Owe + Change! PUT #1, Number%, CustRec ' Unlock the record. UNLOCK #1, Number% LOCATE 17, 10. INPUT "Update another? ", Continue$ Update$ = UCASE$(LEFT$(Continue$, 1)) LOOP WHILE Update$ = "Y" CLOSE #1 KILL "MONITOR"' Remove file from disk. END ErrHandler. IF ERR = 70 THEN ' Permission-denied error. CLS PRINT "You must run SHARE.EXE before running this example." PRINT "Exit the programming environment, run SHARE.EXE, and" PRINT "reenter the programming environment to run this" PRINT "example. Do not shell to DOS to run SHARE.EXE or you" PRINT "may not be able to run other programs until you reboot." END IF END